﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace road2.Model
{
    public enum CarTypes
    {
        none,
        small,
        big
    }

    public class Car : BaseRoadObj
    {
        private Road _road;
        public Road road
        {
            get { return _road; }
            set
            {
                _road = value;
                RaisePropertyChanged("road");
            }
        }

        private int _line;
        public int line
        {
            get { return _line; }
            set
            {
                if (value >= road.lineCount || value < 0)
                {
                    throw new ArgumentOutOfRangeException("line", "invalid line");
                }
                else
                {
                    _line = value;
                }
                RaisePropertyChanged("line");
            }
        }

        private int _distance;
        public int distance
        {
            get { return _distance; }
            private set
            {
                if (value > this.road.length)
                {
                    //throw new ArgumentOutOfRangeException("distance", "distance is greater then road length");
                }
                else
                {
                    _distance = value;
                }
                RaisePropertyChanged("distance");
            }
        }

        private int _speed;
        public int speed
        {
            get { return _speed; }
            private set
            {
                if (value > this.maxSpeed)
                {
                    _speed = this.maxSpeed;
                }
                else if (value < 0)
                {
                    _speed = 0;
                    acceleration = 0;
                }
                else
                {
                    _speed = value;
                }
                RaisePropertyChanged("speed");
            }
        }

        private readonly int _maxSpeed;
        public int maxSpeed
        {
            get { return _maxSpeed; }
            //set
            //{
            //    _maxSpeed = value;
            //    RaisePropertyChanged("maxSpeed");
            //}
        }

        private int _acceleration;
        public int acceleration
        {
            get { return _acceleration; }
            set
            {
                if (value > this.maxAcceleration)
                {
                    this.acceleration = this.maxAcceleration;
                }
                else if (value < this.minAcceleration)
                {
                    this.acceleration = this.minAcceleration;
                }
                else
                {
                    _acceleration = value;
                }
                RaisePropertyChanged("acceleration");
            }
        }

        private readonly int _maxAcceleration;
        public int maxAcceleration
        {
            get { return _maxAcceleration; }
            //set
            //{
            //    _maxAcceleration = value;
            //    RaisePropertyChanged("maxAcceleration");
            //}
        }

        private readonly int _minAcceleration;
        public int minAcceleration
        {
            get { return _minAcceleration; }
            //set
            //{
            //    _minAcceleration = value;
            //    RaisePropertyChanged("minAcceleration");
            //}
        }

        private readonly int _color;
        public int color
        {
            get { return _color; }
            //set
            //{
            //    _color = value;
            //    RaisePropertyChanged("color");
            //}
        }

        private readonly CarTypes _type;
        public CarTypes type
        {
            get { return _type; }
            //set
            //{
            //    _type = value;
            //    RaisePropertyChanged("type");
            //}
        }

        public Car(Guid id, CarTypes type, int color, int maxSpeed, int maxAccel, int minAccel)
            : base(id)
        {
            _color = color;
            _maxSpeed = maxSpeed;
            _minAcceleration = minAccel;
            _maxAcceleration = maxAccel;
            _type = type;
        }

        public void Drive()
        {
            if (this.road == null)
            {
                throw new Exception("car not on road");
            }

            distance += speed + acceleration / 2;
            speed += acceleration;
        }
    }
}
